﻿using Serilog;
using Worldme.YHChatSDK.Common.Constant.Enums;
using Worldme.YHChatSDK.Framework.Vo;

namespace Worldme.YHChatSDK.Framework.Core
{
    internal class GlobalEventHandle
    {
        private static GlobalPluginHandel globalPluginHandel;

        private static GlobalPluginHandel GetGlobalPluginHandel()
        {
            if (globalPluginHandel == null)
            {
                globalPluginHandel = GlobalPluginHandel.GetInstance();
            }
            return globalPluginHandel;
        }


        private EventMsgVo eventMsgVo;

        public GlobalEventHandle(EventMsgVo eventMsgVo)
        {
            this.eventMsgVo = eventMsgVo;
        }

        public Action GetAction()
        {
            return () =>
            {
                string eventType = eventMsgVo.header.eventType;
                if (EventType.MESSAGE_RECEIVE_NORMAL.GetCode().Equals(eventType))
                {
                    EventMessageReceiveNormal(eventMsgVo);
                }
                else if (EventType.MESSAGE_RECEIVE_INSTRUCTION.GetCode().Equals(eventType))
                {
                    EventMessageReceiveInstruction(eventMsgVo);
                }
                else if (EventType.BOT_FOLLOWED.GetCode().Equals(eventType))
                {
                    EventBotFollowed(eventMsgVo);
                }
                else if (EventType.BOT_UNFOLLOWED.GetCode().Equals(eventType))
                {
                    EventBotUnfollowed(eventMsgVo);
                }
                else if (EventType.BUTTON_REPORT_INLINE.GetCode().Equals(eventType))
                {
                    EventButtonReportInline(eventMsgVo);
                }
                else if (EventType.GROUP_JOIN.GetCode().Equals(eventType))
                {
                    EventGroupJoin(eventMsgVo);
                }
                else if (EventType.GROUP_LEAVE.GetCode().Equals(eventType))
                {
                    EventGroupLeave(eventMsgVo);
                }
                else if (EventType.BOT_SHORTCUT_MENU.GetCode().Equals(eventType))
                {
                    EventBotShutcutMenu(eventMsgVo);
                }
                else
                {
                    Log.Error("【YHChatSDK】事件投递失败，未知的消息类型：{eventType}", eventType);
                }
            };
        }


        /// <summary>
        /// 普通消息事件
        /// </summary>
        /// <param name="eventMsgVo"></param>
        public static void EventMessageReceiveNormal(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventMessageReceiveNormalVo> list = GetGlobalPluginHandel().GetEventMessageReceiveNormalVos();
            foreach (PluginManagerVo.EventMessageReceiveNormalVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }

            }
        }

        public static void EventMessageReceiveInstruction(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventMessageReceiveInstructionVo> list = GetGlobalPluginHandel().GetEventMessageReceiveInstructionVos();
            foreach (PluginManagerVo.EventMessageReceiveInstructionVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }

        public static void EventBotFollowed(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventBotFollowedVo> list = GetGlobalPluginHandel().GetEventBotFollowedVos();
            foreach (PluginManagerVo.EventBotFollowedVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }

        public static void EventBotUnfollowed(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventBotUnfollowedVo> list = GetGlobalPluginHandel().GetEventBotUnfollowedVos();
            foreach (PluginManagerVo.EventBotUnfollowedVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }

        public static void EventButtonReportInline(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventButtonReportInlineVo> list = GetGlobalPluginHandel().GetEventButtonReportInlineVos();
            foreach (PluginManagerVo.EventButtonReportInlineVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }

        public static void EventGroupJoin(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventGroupJoinVo> list = GetGlobalPluginHandel().GetEventGroupJoinVos();
            foreach (PluginManagerVo.EventGroupJoinVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }

        public static void EventGroupLeave(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventGroupLeaveVo> list = GetGlobalPluginHandel().GetEventGroupLeaveVos();
            foreach (PluginManagerVo.EventGroupLeaveVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }

        public static void EventBotShutcutMenu(EventMsgVo eventMsgVo)
        {
            List<PluginManagerVo.EventBotShutcutMenuVo> list = GetGlobalPluginHandel().GetEventBotShutcutMenuVos();
            foreach (PluginManagerVo.EventBotShutcutMenuVo vo in list)
            {
                try
                {
                    int integer = vo.Bean.Handle(eventMsgVo);
                    // 如果返回值是-1，那么将不再继续执行
                    if (integer == -1)
                    {
                        return;
                    }
                }
                catch (Exception e)
                {
                    Log.Error("【YHChatSDK】插件[{PluginName}]发送异常：{Message}", vo.PluginName, e.Message);
                }
            }
        }
    }
}
